package com.aote.entity.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.w3c.dom.Element;

import com.aote.mvc.event.Action;
import com.aote.util.StringHelper;
import com.browsesoft.baseadapter.AdvancedSimpleEntity;
import com.browsesoft.dbtools.DBTools;
import com.browsesoft.htmlcomponent.HTMLFormComponent;

/**
 * 执行SQL语句的动作
 */
public class SQLAction extends Action {
	public void performAction(Element config, HttpServletRequest request,
			Map attrs) {
		// 如果是form
		if (this.component instanceof HTMLFormComponent) {
			// 得到模型
			AdvancedSimpleEntity model = (AdvancedSimpleEntity) component
					.getModel();
			attrs = model.getAttributes();
		}
		// 得到SQL语句
		String sql = config.getAttribute("sql");
		// 替换参数为实体属性
		sql = StringHelper.replaceStr(attrs, sql);
		try {
			DBTools.excuteUpdate(sql);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}
